package com.wap.framework.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wap.framework.model.AdminUser;
import com.wap.framework.model.Content;
import com.wap.framework.service.ContentService;
import com.wap.framework.util.Constants;

public class ContentManagerAction extends DispatchAction {

	private ContentService contentService;

	/**
	 * list all contents
	 */
	@SuppressWarnings("unchecked")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String currentPage = request.getParameter("currentPage");
		if(currentPage == null || "".equals(currentPage)) {
			currentPage = "1";
		}		
		
		List<Object> result = contentService.listContent(Integer.valueOf(currentPage));		
		request.setAttribute("contents", (List<Content>)result.get(0));
		request.setAttribute("pageHelper", result.get(1));
		
		return mapping.findForward("fwd_mgr_content");
	}

	/**
	 * edit a content
	 */
	@SuppressWarnings("unchecked")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String contentID = request.getParameter("contentID");
		Content content = contentService
				.editContent(Integer.valueOf(contentID));
		request.setAttribute("content", content);
		return mapping.findForward("fwd_edit_content");
	}

	/**
	 * update a content
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String contentID = request.getParameter("contentID");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String status = request.getParameter("status");

		Content contentBean = new Content();
		contentBean.setId(Integer.valueOf(contentID));
		contentBean.setTitle(title);
		contentBean.setContent(content);
		contentBean.setStatus(Integer.valueOf(status));

		contentService.updateContent(contentBean);
		return list(mapping, form, request, response);
	}

	/**
	 * Add content
	 */
	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {			
		String owner	= "";
		String title 	= request.getParameter("title");
		String sContent = request.getParameter("content");
		String status 	= request.getParameter("status");
		
		HttpSession session = request.getSession();
		AdminUser admin = (AdminUser)session.getAttribute(Constants.ADMIN_TOKEN);
		if(admin != null) {
			owner = admin.getUsername();
		}

		Content content = new Content();	
		content.setOwner(owner);
		content.setTitle(title);
		content.setContent(sContent);
		content.setStatus(Integer.valueOf(status));		
		content.setDelFlag(new Integer(0));
		
		contentService.saveContent(content);
		
		return list(mapping, form, request, response);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] ids = request.getParameterValues("id");		
		int[] contentIds = new int[ids.length];
		for (int i = 0; i < ids.length; i++) {
			contentIds[i] = Integer.valueOf(ids[i]);
		}
		this.contentService.deleteContent(contentIds);
		return list(mapping, form, request, response);
	}

	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}
}
